package com.magicalcoder.youyamvc.web.controller.web.module;
import com.magicalcoder.youyamvc.app.module.service.ModuleService;
import com.magicalcoder.youyamvc.app.module.service.ModuleProxyService;
import com.magicalcoder.youyamvc.app.module.constant.ModuleConstant;
import com.magicalcoder.youyamvc.app.module.util.ModuleUtil;
import com.magicalcoder.youyamvc.web.controller.web.module.param.ModuleRequest;
import com.magicalcoder.youyamvc.web.controller.web.module.param.ModuleResponse;
import com.magicalcoder.youyamvc.app.model.Module;
import com.magicalcoder.youyamvc.core.common.utils.ProjectUtil;
import com.magicalcoder.youyamvc.core.common.utils.MapUtil;
import com.magicalcoder.youyamvc.core.common.utils.ListUtils;
import com.magicalcoder.youyamvc.core.common.utils.StringUtils;
import com.magicalcoder.youyamvc.core.common.dto.AjaxData;
import com.magicalcoder.youyamvc.core.common.utils.copy.Copyer;
import com.magicalcoder.youyamvc.core.spring.admin.AdminLoginController;
import com.magicalcoder.youyamvc.core.spring.web.WebLoginController;
import com.magicalcoder.youyamvc.web.common.BaseController;
import com.magicalcoder.youyamvc.app.userweb.util.UserWebUtil;
import com.magicalcoder.youyamvc.core.common.exception.BusinessException;

import org.springframework.format.annotation.DateTimeFormat;
import java.util.*;
import java.math.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:323237052
*/

@RequestMapping({"/web/module"})
@Controller
public class ModuleListController extends BaseController
{
    @Resource
    private ModuleService moduleService;
    @Resource
    private ModuleProxyService moduleProxyService;

    /** method:GET
    *   url:/web/module/list/{pageIndex}/{pageSize}?...
    *   demo:/web/module/list/1/20
    *   获取模块分页数据
    *   是否需要登录                          是
    *是否必须   入参                          注释
    *  是   @pageIndex                     (Integer)当前页码 [1,n]
    *  是   @pageSize                      (Integer)每页条数 [1,n]
    *  否   @callback                      (String)回调方法
    *  否   @orderBySqlField               (String)排序字段
    *  否   @descAsc                       (String)desc|asc
    *  否   @encode                        (String)编码 默认UTF-8
    *  否   @id                     (Long)主键 
    *  否   @moduleName                     (String)模块唯一键 
    *  否   @moduleUrl                     (String)模块url 
    *  否   @moduleCategoryId                     (Long)模块分类 
    *  否   @sortNum                     (Integer)排序 
    *  否   @moduleTitle                     (String)模块标题 
    *  否   @ifShow                     (Boolean)是否显示 [{"":"全部"},{"true":"是"},{"false":"否"}]
    *  否  like @moduleNameFirst                     (String)模块唯一键 
    *  否  like @moduleUrlFirst                     (String)模块url 
    *  否  in @moduleCategoryIdFirst                     (Long)模块分类 
    *  否  like @moduleTitleFirst                     (String)模块标题 
    *  否   @ifShowFirst                     (Boolean)是否显示 [{"":"全部"},{"true":"是"},{"false":"否"}]
    *  返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:
                [{
    *              id         (Long)主键 
    *              moduleName         (String)模块唯一键 
    *              moduleUrl         (String)模块url 
    *              moduleCategoryId         (Long)模块分类 
    *              sortNum         (Integer)排序 
    *              moduleTitle         (String)模块标题 
    *              ifShow         (Boolean)是否显示 [{"":"全部"},{"true":"是"},{"false":"否"}]
    *      }]
    *   }
    */
    @RequestMapping(value={"list/{pageIndex}/{pageSize}"}, method={RequestMethod.GET})
    public void list(@PathVariable Integer pageIndex,@PathVariable Integer pageSize,
        @RequestParam(required=false, value="orderBySqlField") String orderBySqlField,
        @RequestParam(required=false, value="descAsc") String descAsc,
        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        @RequestParam(required = false,value ="id")   Long id,
        @RequestParam(required = false,value ="moduleName")   String moduleName,
        @RequestParam(required = false,value ="moduleUrl")   String moduleUrl,
        @RequestParam(required = false,value ="moduleCategoryId")   Long moduleCategoryId,
        @RequestParam(required = false,value ="sortNum")   Integer sortNum,
        @RequestParam(required = false,value ="moduleTitle")   String moduleTitle,
        @RequestParam(required = false,value ="ifShow")   Boolean ifShow,
        @RequestParam(required = false,value ="moduleNameFirst")                            String moduleNameFirst ,
        @RequestParam(required = false,value ="moduleUrlFirst")                            String moduleUrlFirst ,
        @RequestParam(required = false,value ="moduleCategoryIdFirst")                            String moduleCategoryIdFirst ,
        @RequestParam(required = false,value ="moduleTitleFirst")                            String moduleTitleFirst ,
        @RequestParam(required = false,value ="ifShowFirst")                            Boolean ifShowFirst ,
        HttpServletRequest request,HttpServletResponse response,ModelMap modelMap)
    {
        try{
            String orderBySqlFieldStr = ",id,module_name,module_url,module_category_id,sort_num,module_title,if_show,";
            String orderBy = ProjectUtil.filterOrderBy(orderBySqlFieldStr,orderBySqlField,descAsc);
            if(StringUtils.isBlank(orderBy)){
                orderBy = "";
            }
            pageSize = Math.min(ModuleConstant.PAGE_MAX_SIZE,pageSize);
            int idx = (pageIndex.intValue() - 1) * pageSize;
            Long _userId = UserWebUtil.userId(request);
            Map<String,Object> query = MapUtil.buildMap("orderBy", orderBy,
         "id",id,
         "moduleName",moduleName,
         "moduleUrl",moduleUrl,
         "moduleCategoryId",moduleCategoryId,
         "sortNum",sortNum,
         "moduleTitle",moduleTitle,
         "ifShow",ifShow,
                    "moduleNameFirst",moduleNameFirst ,
                    "moduleUrlFirst",moduleUrlFirst ,
                    "moduleCategoryIdFirst",ProjectUtil.toList(moduleCategoryIdFirst ,Long.class),
                    "moduleTitleFirst",moduleTitleFirst ,
                    "ifShowFirst",ifShowFirst ,
                "start",idx,"limit", pageSize ,"userId",_userId);

            List<Module> pageList = this.moduleService.getModuleList(query);
            toWebSuccessJson(callback,encode,response, coverToResponseList(pageList));
        }catch(Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(response,exceptionMsg);
        }
    }

    private List<ModuleResponse> coverToResponseList(List<Module> pageList){
        if(ListUtils.isBlank(pageList)){
            return null;
        }
        return Copyer.copy(pageList,ModuleResponse.class);
    }


        /** method:GET
        *   url:/user/module/item?...
        *   demo:/user/module/item
    *   获取模块详情数据
    *   是否需要登录                          是
    *是否必须   入参                          注释
    *  否   @callback                      (String)回调方法
    *  否   @encode                        (String)编码 默认UTF-8
     *  否  @id        (Long) 主键
    *  返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:
                {
    *              id         (Long)主键 
    *              moduleName         (String)模块唯一键 
    *              moduleUrl         (String)模块url 
    *              moduleCategoryId         (Long)模块分类 
    *              sortNum         (Integer)排序 
    *              moduleTitle         (String)模块标题 
    *              ifShow         (Boolean)是否显示 [{"":"全部"},{"true":"是"},{"false":"否"}]
    *      }
    *   }
    */
    @RequestMapping(value={"item"}, method={RequestMethod.GET})
    public void item(
        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        @RequestParam(required = false,value ="id")Long id,
        HttpServletRequest request,HttpServletResponse response,ModelMap modelMap)
    {
        try{
            Long _userId = UserWebUtil.userId(request);
            Map<String,Object> query = MapUtil.buildMap(
                     "id",id,
                "userId",_userId
            );
            Module module = this.moduleService.selectFirstModule(query);
            toWebSuccessJson(callback,encode,response, Copyer.copy(module,new ModuleResponse()));
        }catch(Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(response,exceptionMsg);
        }
    }

}